import { Notification } from 'element-ui'

/**
 * 地图模块 - 风险预警
 */
export default {
    data() {
        return {
            idFXZQ: null,
            legend: [
                // { name: '紫', icon: require('@/assets/map/village/0.png') },
                { name: '红', icon: require('@/assets/map/village/1.png'), color: '#d03319' },
                { name: '橙', icon: require('@/assets/map/village/2.png'), color: '#e18130' },
                { name: '黄', icon: require('@/assets/map/village/3.png'), color: '#efdb3d' },
                { name: '蓝', icon: require('@/assets/map/village/4.png'), color: '#061dd0' }
            ]
        }
    },
    // 计算属性
    computed: {
        // 流域编码
        basin() {
            return this.$store.getters.basin
        }
    },
    // 生命周期-创建
    created() {
        this.entitiesPonitFXZQ = new Map()
        this.screenSpaceEventHandlerFXZQ = null

        this.$eventBus.$on('flyToVillage_FXZQ', this.flyToVillage)
        this.$eventBus.$on('EB_YB_Mesh_CardSel', res => {
            this.idFXZQ = res.id
            if (this.$store.getters.pageState.YJ === 'QX_ZQ') {
                this.creatFXZQ()
            }
        })
        // 监听地图tabchange事件
        this.$eventBus.$on('EB_YJ_TAB_Change', res => {
            console.log(res)
            if (res.selectNum === 0) {
                // this.creatFXZQ()
            } else {
                this.clearFXZQ()
            }
            this.$store.dispatch('closePop')
            this.$store.dispatch('closePopStay')
        })
        this.$eventBus.$on('EB_YJ_QX_BTN_ZQ', res => {
            console.log(res)
            if (res) {
                this.creatFXZQ()
            } else {
                this.clearFXZQ()
            }
            this.$store.dispatch('closePop')
            this.$store.dispatch('closePopStay')
        })
    },
    // 生命周期-销毁前
    beforeDestroy() {
        // this.$eventBus.$off()
    },
    // 生命周期-销毁
    destroyed() {
        // 清理
        this.clearFXZQ()
        // 相关事件监听销毁
        if (this.screenSpaceEventHandlerFXZQ) {
            this.screenSpaceEventHandlerFXZQ.destroy()
            this.screenSpaceEventHandlerFXZQ = null
        }
        this.$eventBus.$off('EB_YJ_TAB_Change')
        this.$eventBus.$off('EB_YJ_QX_BTN_ZQ')
    },
    // 生命周期-挂载
    mounted() {
    },
    methods: {
        /**
         * 模块构建
         * @param {string} strMainId strMainId
         */
        creatFXZQ() {
            this.clearFXZQ()

            // 测试参数
            // strMainId = 'db497406-f325-43eb-91ac-304c0cfd51b9'

            var urlApi = '/api/EarlyWarning/GetWarningAdcd'
            var prarms = {
                wswscd: this.basin,
                strMainId: this.idFXZQ
            }
            this.$api.Get(urlApi, prarms).then(res => {
                if (res.data.length < 1) {
                    Notification({
                        message: `此预警无风险政区`,
                        type: 'warning'
                    })
                    return
                }
                res.data.forEach(item => {
                    var Village = item.village;
                    var lv = Number(item.wLevel) - 1;
                    console.log(item)
                    var color = this.legend[lv].color;
                    var legend = this.legend[lv]
                    var entity = {
                        eventName: 'FXZQ',
                        prop: item,
                        position: Cesium.Cartesian3.fromDegrees(
                            Number(item.lgtd),
                            Number(item.lttd)
                        ),
                        label: {
                            text: Village,
                            scale: 1,
                            font: '14px sans-serif',
                            fillColor: Cesium.Color.fromCssColorString(color),
                            outlineColor: Cesium.Color.fromCssColorString('#FFF'),
                            outlineWidth: 4,
                            style: Cesium.LabelStyle.FILL_AND_OUTLINE,
                            heightReference: Cesium.HeightReference.RELATIVE_TO_GROUND,
                            pixelOffset: new Cesium.Cartesian2(0, -20),
                            eyeOffset: new Cesium.Cartesian3(0, 0, 80),
                            horizontalOrigin: Cesium.HorizontalOrigin.CENTER, // 垂直
                            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                            disableDepthTestDistance: 50000000,
                            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000)
                        },
                        billboard: {
                            image: this.legend[lv].icon,
                            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
                            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
                            heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
                            distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, this.$config.mapConfig.pb_far),
                            width: this.$config.mapConfig.billboardSize,
                            height: this.$config.mapConfig.billboardSize
                        }
                    }
                    this.entitiesPonitFXZQ.set(item.ewcode, this.$viewer.entities.add(entity))
                })
            })
        },

        flyToVillage(adcd) {
            var entity = this.entitiesPonitFXZQ.get(adcd)
            if (entity) {
                this.$viewer.flyTo(entity, {
                    duration: 0.5,
                    offset: {
                        heading: this.$viewer.camera.heading,
                        pitch: this.$viewer.camera.pitch,
                        range: 5000
                    }
                })
            }
        },
        /**
         * 模块清理
         */
        clearFXZQ() {
            if (this.entitiesPonitFXZQ && this.entitiesPonitFXZQ.size > 0) {
                this.entitiesPonitFXZQ.forEach((val, key, map) => {
                    this.$viewer.entities.remove(val)
                    map.delete(key)
                })
                // this.entitiesPonitFXZQ = new Map()
            }
            this.$store.dispatch('closePop')
            this.$store.dispatch('closePopStay')
            if (this.screenSpaceEventHandlerFXZQ) {
                this.screenSpaceEventHandlerFXZQ.destroy()
                this.screenSpaceEventHandlerFXZQ = null
            }
        }
    }
}
